import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import {getUserInfoAPI} from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //用来储存登陆成功之后的token
    token:'',
    //储存服务器返回的客户信息
    userInfo:{
    }
  },
  getters: {
    nickname: state => state.userInfo.nickname, // 昵称
    username: state => state.userInfo.username, // 用户名
    user_pic: state => state.userInfo.user_pic // 用户头像
  },
  mutations: {
    //更新token 的mutations函数
    updateToken(state,val){
      state.token=val
    },
    //更新用户信息
    updataUserInfo(state,val){
      state.userInfo=val
    }
  },
  actions: {
    //请求用户信息
   async getUserInfoActions(store){
    const{data:res} = await  getUserInfoAPI()
    store.commit('updataUserInfo',res.data)
    console.log(res);
    }
  },
  modules: {
  },
  plugins:[
    //注入持久化插件
    createPersistedState()
  ]
})
